<?php
class Permission extends MY_Controller{
	var $permission_codes=array('access'=>1,'add'=>2,'edit'=>3);
	var $permissions=array(1=>'access',2=>'add',3=>'edit');

	function __construct()
	{
		
		parent::__construct();
		
		$this->page=$this->module_codes['Permissions'];
	
	}
	
	function index()
	{
		//get all types to display them
		
        $this->check_page_permission('access');
		$result = $this->STM->get_type();
		
		//if $result['count'] = 0 then there is no data
		//else generate array for all types
		if($result['count'] == 0){
			$data['count'] = 0;
		}
		else{
			$data['count'] = $result['count'];
			$data['result'] = $result['info'];
		}
		$this->load->view('head');
		$this->load->view('permission/main', $data);
		$this->load->view('footer');
	}
	
	function edit_permissions($id=NULL)
	{
		
        $this->check_page_permission('edit');
		$data = $this->MM->get_modules();
		
		
		$pagePermissions=$this->permissions->get_pages_permissions();
		$data['permissions']=$this->permissions->get_permissions();
		$data['pagePermissions']=$pagePermissions;
		$data['groupID']=$id;
		//echo "<pre>"; print_r($data);
			
		$this->load->view('head');
		$this->load->view('permission/permission_form', $data);
		$this->load->view('footer');
	}

   
    // add permissions to a group, each permission must have an input name of "perm1", or "perm2" etc
    function add_permissions()
    {
        // delete all permissions on this groupID first
        
            $this->check_page_permission('edit');
        
        $groupID = $this->input->post('groupID');
        $this->db->where('groupID',$groupID);
        $this->db->delete('permission_map');
        $access = $this->input->post('access');
        $add = $this->input->post('add');
        $edit = $this->input->post('edit');
        // get post
       if(!empty($access))
        foreach ($access as $key => $value)
        {            
                $this->db->set('groupID', $groupID);
                $this->db->set('permissionID', 1);
                $this->db->set('pageID', $key);
                $this->db->insert('permission_map');
            
        }

   if(!empty($add)) 
        foreach ($add as $key => $value)
        {            
                $this->db->set('groupID', $groupID);
                $this->db->set('permissionID', 2);
                $this->db->set('pageID', $key);
                $this->db->insert('permission_map');
            
        }

    if(!empty($edit))
        foreach ($edit as $key => $value)
        {            
                $this->db->set('groupID', $groupID);
                $this->db->set('permissionID', 3);
                $this->db->set('pageID', $key);
                $this->db->insert('permission_map');
            
        }
        

        return $this->index();
    }
   
   

}

/* End of file permission.php.php */
/* Location: ./system/application/controllers/permission.php.php */
